﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win.UltraWinGrid;
using WHC.OrderWater.Commons;
using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using Maticsoft.DBUtility;

namespace CKNetFrameWork
{
    public partial class Frm_ClassRoomPager1 : DockContent, UltraGridInterface, GridSinkInterface
    {
        private GridInterface gif = new GridInterfaceImpl();
        private TrainingDB.BLL.CK_ClassRoomTab m_bllCK_ClassRoomTab = new TrainingDB.BLL.CK_ClassRoomTab();
        private TrainingDB.BLL.CK_SchoolTab m_bllCK_SchoolTab = new TrainingDB.BLL.CK_SchoolTab();
        private Dictionary<string, string> m_dicColName2AliasName = new Dictionary<string, string>();
        private Dictionary<string, string> m_dicColAliasName2Name = new Dictionary<string, string>();
        private string m_filter = "";

        public Frm_ClassRoomPager1()
        {
            InitializeComponent();
        }

      
        public void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            gif.ultraGrid1_BeforeRowsDeleted(sender, e);
        }

        private void 增加一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KeyEventArgs kea = new KeyEventArgs(Keys.Enter | Keys.Control);
            gif.ultraGrid1_KeyDown(sender, kea);
        }

        private void 删除一行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gif.DelUltraGridRow();
        }

        public void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            gif.ultraGrid1_BeforeRowUpdate(sender, e);

        }
        public void ultraGrid1_AfterRowInsert(object sender, RowEventArgs e)
        {
            gif.ultraGrid1_AfterRowInsert(sender, e);
        }
        public void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            gif.ultraGrid1_KeyDown(sender, e);
        }

        private void ShowAllRecordInDataGridView(string strWhere)
        {
            BindData(strWhere);
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            m_filter = "";
            ShowAllRecordInDataGridView(m_filter);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
        }
        public void SearchGoodDetails()
        {
            this.pager1.CurrentPageIndex = 1;
            m_filter = "";
            int nIndex = this.ultraGrid1.ActiveRow.Index;
            List<object> lstO = this.ultraGrid1.DisplayLayout.Bands[0].ColumnFilters.All.ToList();

            string strColumnName;
            string strColumnValue;
            SearchCondition condition = new SearchCondition();
            foreach (object o in lstO)
            {
                ColumnFilter cf = (ColumnFilter)o;
                strColumnName = cf.Column.Key;
                strColumnValue = cf.FilterConditions.Count > 0 ? ((FilterCondition)cf.FilterConditions.All[0]).CompareValue.ToString() : "";
                if (strColumnValue != "")
                {
                    condition = condition.AddCondition(m_dicColAliasName2Name[strColumnName], strColumnValue, SqlOperator.Equal);
                }
            }

            m_filter = condition.BuildConditionSql();
            m_filter = m_filter.Replace("Where", "");
            ShowAllRecordInDataGridView(m_filter);
        }

        public void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            CurrentUGR.Cells["校区名称"].Value = 1;
            CurrentUGR.Cells["教室名称"].Value = "";
            CurrentUGR.Cells["教室编号"].Value = "";
            CurrentUGR.Cells["教室座位行数"].Value = 0;
            CurrentUGR.Cells["教室座位排数"].Value = 0;
            CurrentUGR.Cells["容纳学生数"].Value = 0;
            CurrentUGR.Cells["教室面积"].Value = 0;

            gif.AfterInitNewAddRowValue(nIndex);

        }

        public bool AddGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                TrainingDB.Model.CK_ClassRoomTab modelCK_ClassRoomTab = new TrainingDB.Model.CK_ClassRoomTab();

                modelCK_ClassRoomTab.CK_SchoolTabID = Convert.ToInt32(CurrentUGR.Cells["校区名称"].Value.ToString(), 10);
                modelCK_ClassRoomTab.CK_ClassRoomName = CurrentUGR.Cells["教室名称"].Value.ToString();
                modelCK_ClassRoomTab.CK_ClassRoomNo = CurrentUGR.Cells["教室编号"].Value.ToString();
                modelCK_ClassRoomTab.CK_ClassRoomRows = Convert.ToInt32(CurrentUGR.Cells["教室座位行数"].Value.ToString(), 10);
                modelCK_ClassRoomTab.CK_ClassRoomCols = Convert.ToInt32(CurrentUGR.Cells["教室座位排数"].Value.ToString(), 10);
                modelCK_ClassRoomTab.CK_ClassRoomMaxStudents = Convert.ToInt32(CurrentUGR.Cells["容纳学生数"].Value.ToString(), 10);
                modelCK_ClassRoomTab.CK_ClassRoomArea = Convert.ToDecimal(CurrentUGR.Cells["教室面积"].Value.ToString());


                int nId = m_bllCK_ClassRoomTab.Add(modelCK_ClassRoomTab);
                this.pager1.CurrentPageIndex = 1;
                ShowAllRecordInDataGridView("CK_ClassRoomTabid = '" + nId.ToString() + "'");
      //          MessageBox.Show("添加教室成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {

                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                TrainingDB.Model.CK_ClassRoomTab modelCK_ClassRoomTab = m_bllCK_ClassRoomTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                modelCK_ClassRoomTab.CK_ClassRoomName = CurrentUGR.Cells["教室名称"].Value.ToString();
                modelCK_ClassRoomTab.CK_ClassRoomNo = CurrentUGR.Cells["教室编号"].Value.ToString();
                modelCK_ClassRoomTab.CK_ClassRoomRows = Convert.ToInt32(CurrentUGR.Cells["教室座位行数"].Value.ToString(), 10);
                modelCK_ClassRoomTab.CK_ClassRoomCols = Convert.ToInt32(CurrentUGR.Cells["教室座位排数"].Value.ToString(), 10);
                modelCK_ClassRoomTab.CK_ClassRoomMaxStudents = Convert.ToInt32(CurrentUGR.Cells["容纳学生数"].Value.ToString(), 10);
                modelCK_ClassRoomTab.CK_ClassRoomArea = Convert.ToDecimal(CurrentUGR.Cells["教室面积"].Value.ToString());



                string strBGH = CurrentUGR.Cells["校区名称"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelCK_ClassRoomTab.CK_SchoolTabID = Convert.ToInt32(CurrentUGR.Cells["校区名称"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllCK_SchoolTab.GetList("CK_SchoolTabName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelCK_ClassRoomTab.CK_SchoolTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["CK_SchoolTabID"].ToString(), 10);
                    }
                }


                m_bllCK_ClassRoomTab.Update(modelCK_ClassRoomTab);

               // MessageBox.Show("修改教室成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        public bool DeleteGoodsDetail(int nDeleteID)
        {
            bool bRet = false;

            TrainingDB.Model.CK_ClassRoomTab modelCK_ClassRoomTabOld = m_bllCK_ClassRoomTab.GetModel(nDeleteID);

            if (modelCK_ClassRoomTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return bRet;
            }

            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                try
                {

                    m_bllCK_ClassRoomTab.Delete(nDeleteID);



                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    bRet = true;
                }
            }
            else
            {
                bRet = true;
            }

            return bRet;
        }

        public string SqlValueList(string sql)
        {
            DataSet ds = DbHelperSQL.Query(sql.ToString());
            string strTotal = ds.Tables[0].Rows[0]["Total"].ToString();

            return strTotal;
        }

        /// <summary>    
        /// 执行SQL查询语句，返回所有记录的DataTable集合。    
        /// </summary>    
        /// <param name="sql">SQL查询语句</param>    
        /// <returns></returns>    
        public DataTable SqlTable(string sql)
        {
            DataSet ds = DbHelperSQL.Query(sql.ToString());
            return ds.Tables[0];
        }

        private DataTable FindToDataTable(string where, PagerInfo pagerInfo)
        {
            WHC.Pager.WinControl.PagerHelper helper = new WHC.Pager.WinControl.PagerHelper(@"select CK_ClassRoomTabID
,CK_SchoolTab.CK_SchoolTabName
,CK_ClassRoomName
,CK_ClassRoomNo
,CK_ClassRoomRows
,CK_ClassRoomCols
,CK_ClassRoomMaxStudents
,CK_ClassRoomArea from CK_ClassRoomTab
 join CK_SchoolTab on CK_SchoolTab.CK_SchoolTabid = CK_ClassRoomTab.CK_SchoolTabid ", "*", "CK_ClassRoomTabID", pagerInfo.PageSize, pagerInfo.CurrenetPageIndex, true, where);
            string countSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.SqlServer, true);
            string dataSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.SqlServer, false);

            string value = SqlValueList(countSql);
            pagerInfo.RecordCount = Convert.ToInt32(value);//为了显示具体的信息，需要设置总记录数
            DataTable dt = SqlTable(dataSql);
            this.pager1.InitPageInfo(pagerInfo.RecordCount, pagerInfo.PageSize);
            return dt;
        }

        private void BindData(string strWhere)
        {
            this.ultraGrid1.DataSource = null;
            DataTable dt = FindToDataTable(strWhere, this.pager1.PagerInfo);

            DataSet dsTemp = m_bllCK_SchoolTab.GetOnlyList("");

            this.ultraDropDownDep.DataSource = dsTemp.Tables[0];
            this.ultraDropDownDep.DisplayMember = "校区名称";
            this.ultraDropDownDep.ValueMember = "CK_SchoolTabID";
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownDep.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns["校区名称"].Hidden = false;



            //DataSet ds = m_bllCK_ClassRoomTab.GetList(strWhere);
            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                dt.Columns[strTemp].ColumnName = m_dicColName2AliasName[strTemp];

            }
            this.ultraGrid1.DataSource = dt;

            this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["校区名称"].ValueList = this.ultraDropDownDep;

            this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
        }

        void pager1_PageChanged(object sender, EventArgs e)
        {
            BindData(m_filter);
        }

        void pager1_ExportCurrent(object sender, EventArgs e)
        {
            MessageUtil.ShowTips("导出当前页");
        }

        void pager1_ExportAll(object sender, EventArgs e)
        {
            MessageUtil.ShowTips("导出所有");
        }      
  
        private void Frm_ClassRoom_Load_1(object sender, EventArgs e)
        {
            this.pager1.PageChanged += new WHC.Pager.WinControl.PageChangedEventHandler(pager1_PageChanged);
            this.pager1.ExportCurrent += new WHC.Pager.WinControl.ExportCurrentEventHandler(pager1_ExportCurrent);
            this.pager1.ExportAll += new WHC.Pager.WinControl.ExportAllEventHandler(pager1_ExportAll);

            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
            m_dicColName2AliasName.Add("CK_SchoolTabName", "校区名称");
            m_dicColName2AliasName.Add("CK_ClassRoomName", "教室名称");
            m_dicColName2AliasName.Add("CK_ClassRoomNo", "教室编号");
            m_dicColName2AliasName.Add("CK_ClassRoomRows", "教室座位行数");
            m_dicColName2AliasName.Add("CK_ClassRoomCols", "教室座位排数");
            m_dicColName2AliasName.Add("CK_ClassRoomMaxStudents", "容纳学生数");
            m_dicColName2AliasName.Add("CK_ClassRoomArea", "教室面积");

            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                m_dicColAliasName2Name.Add(m_dicColName2AliasName[strTemp], strTemp);
            }

            gif.SetGridSink(this);
            gif.SetUltraGrid1(this.ultraGrid1);
        }

    }
}
